﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.ObjectModel
{
    using ConnectBasic.Execution;
    using Microsoft.Scripting;
    using DlrExpression = Microsoft.Linq.Expressions.Expression;

        /// <summary>
    /// Represents a unary Not operation.
    /// </summary>
    public sealed class NotExpression : ExpressionBase
    {
        private readonly ExpressionBase mInner;

        /// <summary>
        /// Initializes a new instance of the <see cref="NotExpression" /> class.
        /// </summary>
        /// <param name="inner">The expression to be inverted (can be a null reference if this is an error node).</param>
        /// <param name="expressionSpan">
        /// The span which describes the span in the source text from which the 
        /// expression was derived.
        /// </param>
        public NotExpression(SourceSpan expressionSpan, ExpressionBase inner) 
            : base(expressionSpan)
        {
           mInner  = inner;
        }

        /// <summary>
        /// Gets the expresion which determines the boolean value to be inverted.
        /// </summary>
        public ExpressionBase Inner
        {
            get { return mInner; }
        }

        /// <inheritdoc />
        public override string GetDebuggerDisplay()
        {
            return "Not " + (mInner == null ? "{Null}" : mInner.GetDebuggerDisplay());
        }

        /// <inheritdoc />
        public override DlrExpression Compile(BasicCodeGenerator gen)
        {
            return DlrExpression.Not(GeneratorUtil.Convert(mInner.Compile(gen), typeof(bool)));
        }
    }
}
